package com.yq.ylmall.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.yq.ylmall.constant.ProductConstant;
import com.yq.ylmall.dto.product.CategoryOperateDto;
import com.yq.ylmall.dto.product.RemoveDto;
import com.yq.ylmall.model.product.Category;
import com.yq.ylmall.product.service.ICategoryService;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.service.CacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 商品分类 后台前端控制器
 * </p>
 *
 * @author yzk789
 * @since 2024-05-30
 */
@Api(tags = "商品分类管理接口")
@RestController
@RequestMapping("/admin/product/category")
@RequiredArgsConstructor
public class CategoryAdminController {

    private final ICategoryService categoryService;
    private final CacheService cacheService;

    @ApiOperation(value = "根据parentId获取下级节点")
    @GetMapping(value = "/findByParentId/{parentId}")
    public Result<List<Category>> findByParentId(@PathVariable Long parentId) {
        List<Category> list = categoryService.findByParentId(parentId);
        return Result.build(list , ResultCode.SUCCESS) ;
    }

    @ApiOperation(value = "导出为excel")
    @GetMapping(value = "/export-data")
    public void exportData(HttpServletResponse response) {
        categoryService.exportData(response);
    }

    @ApiOperation(value = "excel导入")
    @PostMapping("/import-data")
    public Result<Object> importData(MultipartFile file) {
        categoryService.importData(file);
        return Result.ok(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "获取全部分类")
    @GetMapping("/list")
    public Result<String> list() {
        return Result.ok(categoryService.findAll());
    }

    @ApiOperation(value = "商品分类操作：有ID更新，无ID新增")
    @PostMapping("/operate")
    public Result<Object> addOrUpdate(@RequestBody CategoryOperateDto dto) {
        boolean success;
        if (dto.getId() != null) {
            success = categoryService.doUpdate(dto);
        } else {
            success = categoryService.save(BeanUtil.copyProperties(dto, Category.class));
            cacheService.delete(ProductConstant.PRODUCT_CATEGORY_CACHE);
        }
        return success ? Result.ok(ResultCode.SAVE_SUCCESS) : Result.fail(ResultCode.SAVE_FAIL);
    }

    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("/remove")
    public Result<Object> delCategoryById(@RequestBody RemoveDto dto) {
        boolean success = categoryService.doRemoveById(dto);
        return success ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

    @ApiOperation(value = "批量删除商品分类")
    @DeleteMapping("/remove/batch")
    public Result<Object> delCategoryByIds(List<Long> ids) {
        boolean success = categoryService.doRemoveByIds(ids);
        return success ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

}


